package com.starblink.guangconfig.ui

import android.content.ClipData
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ClipboardUtils
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.BuildConfig
import com.starblink.android.basic.base.BaseApplication
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.config.GuangConfig
import com.starblink.android.basic.ext.longClick
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.ext.toastUI
import com.starblink.android.basic.network.base.BaseApiClient
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.common.utils.ScanUtil
import com.starblink.basic.route.RoutePage
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.basic.util.CommUtils
import com.starblink.guangconfig.databinding.ActivityGuangUpConfigBinding
import com.starblink.guangconfig.ui.part.*


@Route(path = RoutePage.GCONFIG.GUANG_UP_CONFIG)
class GuangUpConfigActivity : BaseTVMActivity<ActivityGuangUpConfigBinding, GuangUpConfigVM>() {

    override fun initViewBinding(): ActivityGuangUpConfigBinding =
        ActivityGuangUpConfigBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //返回按钮
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        //是否在测试环境切生产 接口没上生产时切生产要报错
        EnvironmentSwitchCiPart.handleVM(mContext, viewModel, viewBinding)
        //环境切换
        EnvironmentSwitchPart.handleVM(mContext, viewModel, viewBinding)
//        //首页添加mock数据
//        HomeFeedsAddPart.handleVM(mContext, viewModel, viewBinding)
//        //首页关键数据请求详细播报
//        HomeDetailToastPart.handleVM(mContext, viewModel, viewBinding)
        //新手引导重置
        NoviceGuidePart.handleVM(mContext, viewModel, viewBinding)
        ImgSearchAnimPart.handleVM(mContext, viewModel, viewBinding)
        //抓包平替功能
        CharlesPart.handleVM(mContext, viewModel, viewBinding)
        //web测试引导
        WebGuidePart.handleVM(this, viewModel, viewBinding)

        viewBinding.otherLog.buildSpannableString {

            addText("当前运行基本配置信息,长按可复制：\n") {
            }
            addText("日期： ${CommUtils.getYearMonthDay()}\n") {
            }
            addText("基本信息： ${AppUtils.getAppVersionName()}/${AppUtils.getAppVersionCode()}/${UserDataCenter.getLocalCountry()}\n") {
            }
            addText("当前环境： ${BaseApiClient.getCurrSvrCfgEnvDesc()} ${BaseApiClient.getCurrSvrCfgEnvName()}\n") {
            }
            addText("当前站点： ${BaseApiClient.getCurrSiteName()}\n") {
            }
            addText("Host： ${BaseApiClient.getCurrSvrCfgEnvHost()}\n") {
            }
            addText("ApolloApi： ${BaseApiClient.getCurrSvrCfgEnvUrl()}\n") {
            }
            addText("H5Host： ${BaseApiClient.getCurrSvrCfgEnvH5Host()}\n") {
            }
            addText("站点上报url： ${BaseApiClient.getCurrSvrCfgTrackerHost()}\n") {
            }
            addText("设备ID： ${UserDataCenter.getDeviceId(viewBinding.otherLog.context)}\n") {
            }
            addText("adid： ${SkStorage.getString(GuangConfig.REPORT_GA_ID, "")}\n") {
            }
            addText("是否在上报点位： ${if (UserDataCenter.successUploadOtherSpm) "是" else "否"}\n") {
            }
            addText("登录状态： ${if (UserDataCenter.isLogin()) "已登录" else "未登录"}\n") {

                setColor(
                    if (UserDataCenter.isLogin()) Color.parseColor("#6DB93B") else Color.parseColor(
                        "#E52E4C"
                    )
                )
            }
            addText("token： ${UserDataCenter.getToken()}\n") {
            }
            addText("用户信息： ${UserDataCenter.getAccountInfo()?.toJson()}\n") {
            }

        }

        viewBinding.otherLog.longClick {
            ClipboardUtils.copyText(viewBinding.otherLog.text)
            toastUI("内容已复制到剪切板")
            true
        }
        viewBinding.btnClearDay.setOnClickListener {
            SkStorage.remove("NAV_BAR_IS_NEW_DAY_1")
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val result = ScanUtil.getScanResult(requestCode, resultCode, data)
        result?.let {
            viewBinding.webGuide.etWebGuide.setText(it)
        }
    }

}